import axios from 'axios'
import Nprogress from 'nprogress'
import { getToken } from '@/utils/auth'

const request = axios.create({
  baseURL: 'https://slwl-api.itheima.net',
  // baseURL: 'https://slwl-geteway.itheima.net',
  timeout: 200000
})

request.interceptors.request.use((config) => {
  Nprogress.start()
  const token = getToken()
  if (token) {
    config.headers.Authorization = token
  }
  return config
}, (error) => {
  return Promise.reject(error)
})

request.interceptors.response.use((response) => {
  Nprogress.done()
  if (response.data.code && response.data.code === 1) {
    return Promise.reject(new Error(response.data.msg))
  }
  return response.data
}, (error) => {
  Nprogress.done()
  return Promise.reject(error)
})

export default request
